package com.lht.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lht.admin.utils.ListUtil;
import com.lht.admin.utils.QueryPlus;
import com.lht.admin.dao.OrderStatisticsDao;
import com.lht.admin.entity.OrderStatisticsEntity;
import com.lht.admin.service.OrderStatisticsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 订单统计表Service实现类
 *
 * @author ZhengZhanWu
 * @date 2020-01-14 23:33:06
 */
@Service("orderStatisticsService")
public class OrderStatisticsServiceImpl extends ServiceImpl<OrderStatisticsDao, OrderStatisticsEntity> implements OrderStatisticsService {

    @Override
    public List<OrderStatisticsEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.id");
        params.put("asc", false);
        Page<OrderStatisticsEntity> page = new QueryPlus<OrderStatisticsEntity>(params).getPage();
        return page.setRecords(baseMapper.selectOrderStatisticsPage(page, params));
    }


    @Override
    public List<OrderStatisticsEntity> getByAgents(List<Long> promoUserIdList) {
        List<Long> fixList = ListUtil.removeNullElement(promoUserIdList);
        if (ListUtil.isEmpty(fixList)) {
            return Collections.emptyList();
        }

        QueryWrapper<OrderStatisticsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(OrderStatisticsEntity.PROMO_USER_ID, fixList);

        return this.list(queryWrapper);
    }

    @Override
    public boolean add(OrderStatisticsEntity orderStatistics) {
        return this.save(orderStatistics);
    }

    @Override
    public boolean update(OrderStatisticsEntity orderStatistics) {
        return this.updateById(orderStatistics);
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
}
